package com.wgchao.amc.controller.base;

import com.wgchao.amc.autho.AuthoInfoCenter;
import com.wgchao.amc.common.BindingError;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.PageData;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.common.validate.Update;
import com.wgchao.amc.mapstruct.dto.SysBaseRoleDTO;
import com.wgchao.amc.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: RoleController
 * @author: wgchao
 * @createTime: 2019/10/29 9:12 AM
 * @Description: 角色管理
 */
@RestController
@RequestMapping("/amc/sys/base/role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/save")
    @ApiOperation(value = "新增角色")
    @MenuAutho(desc = "新增角色")
    @ApiParam(name = "roleDTO", value = "角色信息", required = true)
    public ResultCommon<SysBaseRoleDTO> save(@RequestBody @Validated(value = {Save.class}) SysBaseRoleDTO roleDTO,
                                             BindingResult bindingResult,
                                             HttpSession session) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return roleService.save(roleDTO, authoInfoCenter);
    }

    @GetMapping("/search/{roleId}")
    @ApiOperation(value = "查询角色详细信息")
    @MenuAutho(desc = "查询角色详细信息")
    @ApiParam(name = "roleId", value = "角色ID", required = true)
    public ResultCommon<SysBaseRoleDTO> search(@PathVariable Long roleId) throws Exception{
        return roleService.search(roleId);
    }

    @GetMapping("/search/by/{agencyCode}")
    @ApiOperation(value = "查询机构下角色列表")
    @MenuAutho(desc = "查询机构下角色列表")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<List<SysBaseRoleDTO>> search(@PathVariable String agencyCode) throws Exception{
        return roleService.search(agencyCode);
    }

    @GetMapping("/search/group/{groupId}")
    @ApiOperation(value = "查询部门下角色列表")
    @MenuAutho(desc = "查询部门下角色列表")
    @ApiParam(name = "groupId", value = "部门ID", required = true)
    public ResultCommon<List<SysBaseRoleDTO>> searchofGroup(@PathVariable Long groupId) throws Exception{
        return roleService.searchofGroup(groupId);
    }

    @GetMapping("/search/user/{userId}/{groupId}")
    @ApiOperation(value = "查询用户拥有的部门职务")
    @MenuAutho(desc = "查询用户拥有的部门职务")
    @ApiParam(name = "groupId", value = "部门ID", required = true)
    public ResultCommon<List<SysBaseRoleDTO>> searchofUser(@PathVariable Long userId, @PathVariable Long groupId) throws Exception{
        return roleService.searchofUser(userId, groupId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新角色信息", notes = "修改角色名称")
    @MenuAutho(desc = "更新角色信息")
    @ApiParam(name = "roleDTO", value = "角色信息", required = true)
    public ResultCommon<SysBaseRoleDTO> update(@RequestBody @Validated(value = {Update.class}) SysBaseRoleDTO roleDTO,
                                               BindingResult bindingResult,
                                               HttpSession session) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return roleService.update(roleDTO, authoInfoCenter);
    }

    @DeleteMapping("/deletes")
    @ApiOperation(value = "删除角色信息")
    @MenuAutho(desc = "删除角色信息")
    @ApiParam(name = "roleIds", value = "角色ID列表", required = true)
    public ResultCommon delete(@RequestBody List<Long> roleIds, HttpSession session) throws Exception{
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return roleService.delete(roleIds, authoInfoCenter);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询角色信息")
    @MenuAutho(desc = "分页查询角色信息")
    public ResultCommon<PageData<SysBaseRoleDTO>> page(int page, int limit,
                                                       @RequestParam(required = true) String agencyCode,
                                                       @RequestParam(required = false) String roleName) throws Exception{
        return roleService.page(page, limit, agencyCode, roleName);
    }


}
